package kumiyunwii.kumi.util;

import kumiyunwii.kumi.config.ElasticsearchConfig;
import org.elasticsearch.action.DocWriteResponse;
import org.elasticsearch.action.admin.indices.analyze.AnalyzeRequestBuilder;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
import org.elasticsearch.action.bulk.BulkItemResponse;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.IndicesAdminClient;
import org.elasticsearch.client.Requests;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import java.util.ResourceBundle;
import java.util.concurrent.ExecutionException;

import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;

public class ElasticSearchUtil {
    private static TransportClient client;
    public ElasticSearchUtil() {
        this.client=new ElasticsearchConfig().getElasticsearchClient();  //使用上面创建好的客户端添加到类中。
    }

    //创建索引，并给索引某些字段指定iK分词，以后向该索引中查询时，就会用ik分词。
    public static void createIndex() throws IOException {
        //创建映射
        XContentBuilder mapping = XContentFactory.jsonBuilder()
                .startObject()
                .startObject("properties")
//                .startObject("m_id").field("type","keyword").endObject()
// 　　             title:字段名，  type:文本类型       analyzer ：分词器类型
                .startObject("title").field("type", "text").field("analyzer", "ik_smart").endObject()   //该字段添加的内容，查询时将会使用ik_smart分词
                .startObject("content").field("type", "text").field("analyzer", "ik_max_word").endObject()
                .endObject()
                .endObject();

        //index：索引名   type：类型名（可以自己定义）
        PutMappingRequest putmap = Requests.putMappingRequest("index").type("type").source(mapping);
        //创建索引
        client.admin().indices().prepareCreate("index").execute().actionGet();
        //为索引添加映射
        client.admin().indices().putMapping(putmap).actionGet();
    }
    public static void createIndex1() throws IOException {
        IndexResponse response = client.prepareIndex("index", "type", "1") //索引，类型，id
                .setSource(jsonBuilder()
                        .startObject()
                        .field("title", "title")   //字段，值
                        .field("content", "content")
                        .endObject()
                ).get();
    }
}
